package top.aiprogram.common.utils;


import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtils {
    private static String driver = "com.mysql.jdbc.Driver";
    private static String url = "jdbc:mysql://192.168.100.240:3306/studentmanage?useUnicode=true&characterEncoding=UTF-8";
    private static String user = "root";
    private static String password = "123456";

    public static Connection getConn() throws PropertyVetoException, SQLException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setJdbcUrl(url);
        dataSource.setDriverClass(driver);
        dataSource.setUser(user);
        dataSource.setPassword(password);

        Connection conn = dataSource.getConnection();
        return conn;
    }

    public static void close(ResultSet resultSet, PreparedStatement preparedStatement, Connection connection) throws SQLException {
        if(resultSet!=null){
            resultSet.close();
        }

        if(preparedStatement!=null){
            preparedStatement.close();
        }

        if(connection!=null){
            connection.close();
        }
    }
}
